package com.logictech.lims.web.back;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.util.SystemTools;
import org.jeecgframework.p3.core.util.plugin.ContextHolderUtils;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.logictech.activity.system.vo.LoginUser;
import com.logictech.common.util.data.ExcelUtil;
import com.logictech.lims.dto.GlasswareDto;
import com.logictech.lims.entity.Glassware;
import com.logictech.lims.entity.GlasswareType;
import com.logictech.lims.service.GlasswareService;
import com.logictech.lims.service.GlasswareTypeService;
import com.logictech.lims.service.LogService;

/**
 * 描述：</b>GlasswareController<br>
 * 
 * @author guoqi
 * @since：2017年03月23日 13时02分39秒 星期四
 * @version:1.0
 */
@Controller
@RequestMapping("/lims/back/glassware")
public class GlasswareController extends BaseController {
	@Autowired
	private GlasswareService glasswareService;
	
	@Autowired
	private GlasswareTypeService glasswareTypeService;
	
	@Autowired
	private LogService logService;

	/**
	 * 列表页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "list", method = { RequestMethod.GET, RequestMethod.POST })
	public void list(@ModelAttribute GlasswareDto query, HttpServletResponse response, HttpServletRequest request,
			@RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception {
		PageQuery<GlasswareDto> pageQuery = new PageQuery<GlasswareDto>();
		pageQuery.setPageNo(pageNo);
		pageQuery.setPageSize(pageSize);
		VelocityContext velocityContext = new VelocityContext();
		pageQuery.setQuery(query);
		velocityContext.put("glassware", query);
		velocityContext.put("pageInfos", SystemTools.convertPaginatedList(glasswareService.queryPageList(pageQuery)));
		String viewName = "lims/back/glassware-list.vm";
		request.getSession().setAttribute("pageQueryGlasswareDto", pageQuery);
		ViewVelocity.view(request, response, viewName, velocityContext);
	}
	
	/**
	 * 返回列表页面
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "back", method = { RequestMethod.GET, RequestMethod.POST })
	public void back(@ModelAttribute GlasswareDto query, HttpServletResponse response, HttpServletRequest request,
			@RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception {
		PageQuery<GlasswareDto> pageQuery = new PageQuery<GlasswareDto>();
		
		Object session = request.getSession().getAttribute("pageQueryGlasswareDto");
		if(session != null){
			pageQuery = (PageQuery<GlasswareDto>)session;
			query = pageQuery.getQuery();
		}else{
			pageQuery.setPageNo(pageNo);
			pageQuery.setPageSize(pageSize);
			pageQuery.setQuery(query);
		}
		
		VelocityContext velocityContext = new VelocityContext();
		velocityContext.put("glassware", query);
		velocityContext.put("pageInfos", SystemTools.convertPaginatedList(glasswareService.queryPageList(pageQuery)));
		String viewName = "lims/back/glassware-list.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}
	
	/**
	 * 导出excel
	 * 
	 * @return
	 */
	@RequestMapping(value = "export", method = { RequestMethod.GET, RequestMethod.POST })
	public void list(@ModelAttribute GlasswareDto query, HttpServletResponse response, HttpServletRequest request) throws Exception {
		try{
			LoginUser user = (LoginUser) ContextHolderUtils.getSession().getAttribute("OPERATE_WEB_LOGIN_USER");
			logService.doAdd(user.getUserName() + "导出玻璃仪器信息");
			
			PageQuery<GlasswareDto> pageQuery = new PageQuery<GlasswareDto>();
			pageQuery.setQuery(query);
			List<GlasswareDto> glasswareDtoList = glasswareService.queryList(pageQuery);
			LinkedHashMap<String, String> fieldMap = new LinkedHashMap<String, String>();
			fieldMap.put("no", "编号");
			fieldMap.put("type", "类型");
			fieldMap.put("size", "规格(ml)");
			fieldMap.put("status", "状态");
			fieldMap.put("createTimeExp", "入库时间");
			fieldMap.put("checkTimeExp", "校验完成时间");
			fieldMap.put("scrapTimeExp", "报废时间");
			fieldMap.put("scrapReason", "报废原因");
			ExcelUtil.listToExcel(glasswareDtoList, fieldMap, "玻璃仪器信息", response);
		}catch(Exception e){
			System.out.println(e.getStackTrace());
		}
	}

	/**
	 * 详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "toDetail", method = RequestMethod.GET)
	public void toDetail(@RequestParam(required = true, value = "id") String id, HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		String viewName = "lims/back/glassware-detail.vm";
		Glassware glassware = glasswareService.queryById(id);
		velocityContext.put("glassware", glassware);
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	/**
	 * 跳转到添加页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/toAdd", method = { RequestMethod.GET, RequestMethod.POST })
	public void toAdd(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		String viewName = "lims/back/glassware-add.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	/**
	 * 保存信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/doAdd", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public AjaxJson doAdd(@ModelAttribute GlasswareDto glassware) {
		AjaxJson j = new AjaxJson();
		try {
			String no = glassware.getNo();
			if(glasswareService.countByNo(no)>0){
				j.setSuccess(false);
				j.setMsg("编号已存在");
				return j;
			}
			
			// 编号规则
			//String year = DateUtils.formatString(new Date(), "yy-");
			GlasswareType glasswareType = glasswareTypeService.queryById(glassware.getGlasswareTypeId().toString());
			//String rule = glasswareType.getRule();
			
			String strGlasswareType = "";
			strGlasswareType += glasswareType.getSize() + "ml";
			if("1".equals(glasswareType.getType())){
				strGlasswareType += "容量瓶";
			}else if("2".equals(glasswareType.getType())){
				strGlasswareType += "移液管";
			}else if("3".equals(glasswareType.getType())){
				strGlasswareType += "刻度吸管";
			}else if("4".equals(glasswareType.getType())){
				strGlasswareType += "量筒";
			}
			
			LoginUser user = (LoginUser) ContextHolderUtils.getSession().getAttribute("OPERATE_WEB_LOGIN_USER");
			glassware.setCreateUser(user.getId().toString());
			glassware.setCreateTime(new Date());
			glassware.setUpdateUser(user.getId().toString());
			glassware.setUpdateTime(new Date());
			glassware.setGlasswareType(strGlasswareType);
			glasswareService.doAdd(glassware);
			
			// 查询db中的最大编号
//			int maxNo = glasswareService.getMaxNo(year+rule);
//			int num = glassware.getNum()==null?1:glassware.getNum();
//			if(num==1){
//				maxNo++;
//				if(maxNo<10){
//					glassware.setNo(year+rule+'0'+maxNo);
//				}else{
//					glassware.setNo(year+rule+maxNo);
//				}
//				glasswareService.doAdd(glassware);
//			}else{
//				for(int i=0;i<num;i++){
//					maxNo++;
//					if(maxNo<10){
//						glassware.setNo(year+rule+'0'+maxNo);
//					}else{
//						glassware.setNo(year+rule+maxNo);
//					}
//					glasswareService.doAdd(glassware);
//				}
//			}
			j.setMsg("保存成功");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("保存失败");
		}
		return j;
	}

	/**
	 * 跳转到编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "toEdit", method = RequestMethod.GET)
	public void toEdit(@RequestParam(required = true, value = "id") String id, HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		GlasswareDto glasswareDto = (GlasswareDto) glasswareService.queryById(id);
		velocityContext.put("glassware", glasswareDto);

		//规格列表
		List<GlasswareType> sizeList = new ArrayList<GlasswareType>();
		sizeList = this.glasswareTypeService.querySizeList(glasswareDto.getType());
		velocityContext.put("sizeList", sizeList);

		String viewName = "lims/back/glassware-edit.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	/**
	 * 编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "/doEdit", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public AjaxJson doEdit(@ModelAttribute Glassware glassware) {
		AjaxJson j = new AjaxJson();
		try {
			LoginUser user = (LoginUser) ContextHolderUtils.getSession().getAttribute("OPERATE_WEB_LOGIN_USER");
			glassware.setUpdateUser(user.getId().toString());
			glassware.setUpdateTime(new Date());
			
			
			GlasswareType glasswareType = glasswareTypeService.queryById(glassware.getGlasswareTypeId().toString());
			String strGlasswareType = "";
			strGlasswareType += glasswareType.getSize() + "ml";
			if("1".equals(glasswareType.getType())){
				strGlasswareType += "容量瓶";
			}else if("2".equals(glasswareType.getType())){
				strGlasswareType += "移液管";
			}else if("3".equals(glasswareType.getType())){
				strGlasswareType += "刻度吸管";
			}else if("4".equals(glasswareType.getType())){
				strGlasswareType += "量筒";
			}
			
			glassware.setGlasswareType(strGlasswareType);
			glasswareService.doEdit(glassware);
			j.setMsg("编辑成功");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("编辑失败");
		}
		return j;
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "doDelete", method = RequestMethod.GET)
	@ResponseBody
	public AjaxJson doDelete(@RequestParam(required = true, value = "id") String id) {
		AjaxJson j = new AjaxJson();
		try {
			glasswareService.doDelete(id);
			j.setMsg("删除成功");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("删除失败");
		}
		return j;
	}

	
}
